<template>
  <div style="margin: 20px auto; text-align: center; width: 80%;">
    <h2 style="text-align: left; margin-left: 20px;color: #d7aa7f">我的订单</h2>
    <div style="border: 1px solid rgba(0, 0, 0, 0.08); padding: 20px;">
      <el-menu
          :default-active="activeIndex"
          active-text-color="#D2B599FF"
          class="el-menu-demo"
          mode="horizontal"
          text-color="#2c3e50"
          @select="handleSelect"
      >
        <el-menu-item index="">全部订单</el-menu-item>
        <el-menu-item index="0">待付款</el-menu-item>
        <el-menu-item index="1">待发货</el-menu-item>
        <el-menu-item index="2">待收货</el-menu-item>
        <el-menu-item index="3">已完成</el-menu-item>
        <el-menu-item index="4">已取消</el-menu-item>
      </el-menu>
      <div v-for="order in orders" :key="order.id" class="order-container"
           style="margin:20px 0px 0px 10px; border-bottom: 1px solid antiquewhite; position: relative;">
        <div class="order-details"
             style="display: flex; justify-content: space-between; align-items: center; line-height: 60px">
          <div class="order-image-wrapper" style="flex: 1; max-width: 200px;">
            <img :src="defaultImg" alt="商品图片" style="width: 100%; height: auto;">
            <!--            <img :src="order.itemImage" alt="商品图片" style="width: 100%; height: auto;">-->
          </div>
          <div class="order-info-wrapper" style="display: flex;">
            <div class="order-title" style="font-size: 18px; font-weight: bold;">{{ order.itemName }}</div>
            <div class="order-price" style="margin-top: 10px;">
              单价: ￥{{ order.price }}
            </div>
            <div class="order-quantity" style="margin-top: 10px;">
              数量: {{ order.quantity }}
            </div>
          </div>
        </div>
        <div class="order-summary" style="display: flex; justify-content: flex-end; align-items: center; margin-bottom: 20px;">
          <div class="total-price" style="font-size: 16px; font-weight: bold;">总价: <span
              style="color: #d20415">￥{{ order.total }}</span></div>
        </div>
        <div class="order-status"
             style="position: absolute; right: 0; top: 0; font-size: 18px;color: #8c939d;padding: 10px;font-family:'Microsoft YaHei';">
          {{ statusText(order.status) }}
        </div>
        <div v-if="order.status === '0'" class="order-actions">
          <el-button type="primary" @click="showPaymentDialog(order.id)">去付款</el-button>
          <el-button type="info" @click="cancelOrder(order.id)">取消订单</el-button>
        </div>
        <div v-if="order.status === '1'" class="order-actions">
          <el-button type="danger" @click="cancelOrder(order.id)">取消订单</el-button>
        </div>
        <div v-if="order.status === '2'" class="order-actions">
          <el-button type="primary" @click="receiveOrder(order.id)">去收货</el-button>
        </div>
      </div>
    </div>
    <!-- 弹框 -->
    <el-dialog :visible.sync="paymentDialogVisible" title="确认付款">
      <div style="font-size: 18px">您确定要付款吗？</div>
      <span slot="footer" class="dialog-footer">
      <el-button @click="paymentDialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="confirmPayment(orderId)">确 认</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "orders",
  data() {
    return {
      activeIndex: "", // 当前选中的菜单项
      orders: [], // 存储订单数据
      paymentDialogVisible: false,
      defaultImg: require('@/assets/items/default.jpg')
    };
  },
  methods: {
    fetchOrders(status) {
      axios.get(`/itemOrder/webFind?status=${status || ''}`)
          .then(response => {
            this.orders = response.data.map(order => ({
              ...order,
              itemName: order.item.name,
              itemImage: order.item.imgUrl,
              size: order.item.size,
              price: order.item.price,
              quantity: 1, // 假设每个订单只有一个商品
              total: parseFloat(order.total),
            }));
          })
          .catch(error => {
            console.error('There was an error fetching the orders:', error);
          });
    },

    handleSelect(key) {
      this.activeIndex = key;
      this.fetchOrders(key || '');
    },
    statusText(status) {
      switch (status) {
        case '0':
          return '待付款';
        case '1':
          return '待发货';
        case '2':
          return '待收货';
        case '3':
          return '已完成';
        case '4':
          return '已取消';
        default:
          return '未知状态';
      }
    },
    goToPayment(orderId) {
      // 这里可以添加跳转到付款页面的逻辑
      // 取消订单
      axios.put(`/itemOrder/pay/${orderId}`, {})
          .then(response => {
            console.log('Order cancelled successfully', response);
            this.fetchOrders(); // 更新订单数据
          })
          .catch(error => {
            console.error('There was an error cancelling the order:', error);
          });
    },
    showPaymentDialog(orderId) {
      this.paymentDialogVisible = true;
      this.orderId = orderId;
    },
    confirmPayment(orderId) {
      this.goToPayment(orderId);
      this.paymentDialogVisible = false;
    },
    cancelOrder(orderId) {
      // 取消订单
      axios.put(`/itemOrder/cancel/${orderId}`, {})
          .then(response => {
            console.log('Order cancelled successfully', response);
            this.fetchOrders(); // 更新订单数据
          })
          .catch(error => {
            console.error('There was an error cancelling the order:', error);
          });
    },

    receiveOrder(orderId) {
      // 收货订单
      axios.put(`/itemOrder/receive/${orderId}`, {})
          .then(response => {
            console.log('Order received successfully', response);
            this.fetchOrders(); // 更新订单数据
          })
          .catch(error => {
            console.error('There was an error receiving the order:', error);
          });
    }
  },
  mounted() {
    this.fetchOrders();
  }
}
</script>

<style scoped>
.order-container {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.order-details {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 20px;
}

.order-image-wrapper {
  flex: 1;
  max-width: 200px;
}

.order-image-wrapper img {
  width: 100%;
  height: auto;
  border-radius: 4px;
}

.order-info-wrapper {
  display: flex;
  flex-direction: column;
  margin-left: 50px;
  margin-top: 20px;
  align-items: flex-start;
}

.order-title {
  font-size: 16px;
  font-weight: bold;
}

.order-size, .order-price, .order-quantity {
  margin-top: 10px;
}

.order-summary {
  display: flex;
  align-items: center;
}

.total-price, .order-status {
  font-size: 16px;
}

.order-actions {
  position: absolute;
  right: 0;
  bottom: 0;
  margin-bottom: 20px;
}
</style>
